﻿namespace Probel.PersonalNotes.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.UI.DataVisualization.Charting;

    using Probel.PersonalNotes.Models.Home;

    public class ChartsController : Controller
    {
        #region Fields

        public const int DefaultHeight = 600;
        public const int DefaultWidth = 900;

        private readonly RecordManager Manager = new RecordManager();

        #endregion Fields

        #region Properties

        private static StatisticsContainer Statistics
        {
            get;
            set;
        }

        #endregion Properties

        #region Methods

        public ActionResult GetTotalByYear(StatisticsContainer model)
        {
            try
            {
                if (Statistics == null) { return View(); }

                var chart = new Chart()
                {

                    Height = DefaultHeight,
                    Width = DefaultWidth,
                    BackHatchStyle = ChartHatchStyle.None
                };

                var area = new ChartArea();
                area.AxisX.MajorGrid.Enabled = false;
                area.AxisY.MajorGrid.Enabled = false;
                area.AxisX.Interval = 1;
                // configure your chart area (dimensions, etc) here.
                chart.ChartAreas.Add(area);

                // create and customize your data series.
                var series = new Series();
                foreach (var item in Statistics.SummaryByYear)
                {
                    series.Points.AddXY(item.Year, item.Count);
                }
                series.Label = "#VAL{0}";
                series.Font = new Font("Segoe UI", 8.0f, FontStyle.Bold);
                series.Color = ColorTranslator.FromHtml("#5c87b2");
                series.ChartType = SeriesChartType.Column;
                series["PieLabelStyle"] = "Outside";

                chart.Series.Add(series);

                var returnStream = new MemoryStream();
                chart.ImageType = ChartImageType.Png;
                chart.SaveImage(returnStream);
                returnStream.Position = 0;
                return new FileStreamResult(returnStream, "image/png");
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public ActionResult GetTotalByYearAndResults(StatisticsContainer model)
        {
            if (Statistics == null) { return View(); }

            var chart = new Chart()
            {
                Height = DefaultHeight,
                Width = DefaultWidth,
                BackHatchStyle = ChartHatchStyle.None
            };

            var area = new ChartArea();
            area.AxisX.MajorGrid.Enabled = false;
            area.AxisY.MajorGrid.Enabled = false;
            area.AxisX.Interval = 1;
            // configure your chart area (dimensions, etc) here.
            chart.ChartAreas.Add(area);

            chart.Series.Add(GetKcSerie());
            chart.Series.Add(GetBcSerie());
            chart.Series.Add(GetFcSerie());

            var returnStream = new MemoryStream();
            chart.ImageType = ChartImageType.Png;
            chart.SaveImage(returnStream);
            returnStream.Position = 0;
            return new FileStreamResult(returnStream, "image/png");
        }

        // GET: Chart
        public ActionResult Index()
        {
            var result = this.Manager.GetSummaryContainer();
            Statistics = result;
            return View(result);
        }

        private static Series GetBcSerie()
        {
            // create and customize your data series.
            var series = new Series();
            foreach (var item in Statistics.SummaryByYear)
            {
                series.Points.AddXY(item.Year, item.BC);
            }

            series.Label = "BC: #VAL{0}";
            series.Font = new Font("Segoe UI", 8.0f, FontStyle.Bold);
            series.Color = ColorTranslator.FromHtml("#FAAC58");
            series.ChartType = SeriesChartType.StackedColumn100;
            series["PieLabelStyle"] = "Outside";

            foreach (var point in series.Points)
            {
                if (point.YValues.Length > 0 && (double)point.YValues.GetValue(0) == 0)
                {
                    point.Label = string.Empty;
                }
            }
            return series;
        }

        private static Series GetFcSerie()
        {
            // create and customize your data series.
            var series = new Series();
            foreach (var item in Statistics.SummaryByYear)
            {
                series.Points.AddXY(item.Year, item.FC);
            }

            series.Label = "FC: #VAL{0}";
            series.Font = new Font("Segoe UI", 8.0f, FontStyle.Bold);
            series.Color = ColorTranslator.FromHtml("#81F781");
            series.ChartType = SeriesChartType.StackedColumn100;
            series["PieLabelStyle"] = "Outside";

            foreach (var point in series.Points)
            {
                if (point.YValues.Length > 0 && (double)point.YValues.GetValue(0) == 0)
                {
                    point.Label = string.Empty;
                }
            }
            return series;
        }

        private static Series GetKcSerie()
        {
            // create and customize your data series.
            var series = new Series();
            foreach (var item in Statistics.SummaryByYear)
            {
                series.Points.AddXY(item.Year, item.KC);
            }

            series.Label = "KC: #VAL{0}";
            series.Font = new Font("Segoe UI", 8.0f, FontStyle.Bold);
            series.Color = ColorTranslator.FromHtml("#F5A9A9");
            series.ChartType = SeriesChartType.StackedColumn100;
            series["PieLabelStyle"] = "Outside";

            foreach (var point in series.Points)
            {
                if (point.YValues.Length > 0 && (double)point.YValues.GetValue(0) == 0)
                {
                    point.Label = string.Empty;
                }
            }

            return series;
        }

        #endregion Methods
    }
}